package com.white.commonlib.crash;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import com.white.commonlib.CommonEngine;
import com.white.commonlib.base.AppContext;
import com.white.commonlib.engine.ApiDecoder;
import com.white.commonlib.manager.LogManager;
import com.white.commonlib.manager.net.HttpManager;
import com.white.commonlib.manager.net.ResultCallback;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UploadCrashLogService extends Service
{

    private HttpManager manager;

    public UploadCrashLogService()
    {
    }

    private Map<String,String> header;

    @Override
    public void onCreate()
    {
        super.onCreate();
        manager = HttpManager.getInstance();
    }

    int totle;
    List<CrashBean> uploadQueue;
    boolean isOnUploading;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        LogManager.e("进来了 upload log service");
        if(!isOnUploading)
        {
            uploadQueue = AppContext.getContext().getDbHelper().list();
            if(uploadQueue != null && uploadQueue.size() > 0)
            {
                isOnUploading = true;
                totle = uploadQueue.size() -1;
                upload(uploadQueue.get(totle));
            }else
            {
                stopSelf();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

//    String url = "http://119.23.35.212:8080/MavenDemo/upload/uploadLog";

    private void upload(final CrashBean cb)
    {
        String url = AppContext.getContext().getAppConfig().getUploadAddress();
        final File f = new File(cb.logFilePath);
        if(f.exists())
        {
            createHeader();
            manager.uploadSingleFile(url, header,"crashlog",f,
                    false,new ResultCallback<String>()
                    {
                        @Override
                        public void onError(String simpleMsg, int code, Exception e)
                        {
                            LogManager.e("上传失败");
                            totle = 0;
                            isOnUploading = false;
                            stopSelf();
                        }

                        @Override
                        public void onSuccess(String response)
                        {
                            LogManager.e("上传成功");
                            f.delete();
                            cb.isUploaded = 1;
                            AppContext.getContext().getDbHelper().update(cb);
                            nextUploadTask();
                        }
                    });
        }else
        {
            nextUploadTask();
        }
    }

    private void createHeader()
    {
        if(header == null)
        {
            header = new HashMap<>();
            header.put("userid", "-99");
            header.put("client", "sxb");
            header.put("Brand", Build.BRAND);
            header.put("DeviceModel", Build.MODEL);
            header.put("ApiCode", ApiDecoder.getVersionName(Build.VERSION.SDK_INT));
            header.put("logtype", "1");
//            header.put("userid", String.valueOf((Long) PreferenceUtils.get(Constant.PREF_REAL_USER_ID, -1L)));
        }
    }

    private void nextUploadTask()
    {
        totle--;
        if(totle < 0)
        {
            totle = 0;
            isOnUploading = false;
            stopSelf();
        }else
        {
            upload(uploadQueue.get(totle));
        }
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
